package com.googlecode.connectlet.machine.provider.cipher;

import java.security.InvalidKeyException;
import java.security.Key;

import com.googlecode.connectlet.machine.provider.key.MachinePrivateKey;

public final class MachineRSACipher extends MachineCipher {
	private MachinePrivateKey privateKey;

	@Override
	protected void init(int opmode, Key key) throws InvalidKeyException {
		if (key instanceof MachinePrivateKey) {
			privateKey = (MachinePrivateKey) key;
		} else {
			throw new InvalidKeyException("MachinePrivateKey required");
		}
		block = new byte[(privateKey.getKeySize() + 7) / 8];
	}

	@Override
	protected byte[] doFinal(byte[] data) {
		return machineKey.decrypt(privateKey, data);
	}
}